package com.etc.layui.controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.etc.layuisys.entity.Users;
import com.etc.layuisys.service.UsersService;
import com.etc.layuisys.service.impl.UsersServiceImpl;
import com.etc.layuisys.utils.CommonMessage;
import com.etc.layuisys.utils.PageData;
import com.google.gson.Gson;

/**
 * Servlet implementation class UsersServlet
 */
@WebServlet("/Users/list")
public class UsersServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private UsersService usersService = new UsersServiceImpl();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public UsersServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		String op = "doList";
		if (request.getParameter("op") != null) {
			op = request.getParameter("op");
		}

		if (op.equals("doList")) {
			doList(request, response);
		}
		if (op.equals("del")) {
			doDelete(request, response);
		}
		if (op.equals("updateUser")) {
			doUpdateUser(request, response);
		}
	}

	/**
	 * 分页+模糊查询
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void doList(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/json");
		PrintWriter out = response.getWriter();

		Integer page = 1;
		Integer limit = 10;
		String content = "";
		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}
		if (request.getParameter("limit") != null) {
			limit = Integer.parseInt(request.getParameter("limit"));
		}
		if (request.getParameter("content") != null) {
			content = request.getParameter("content");
		}

		PageData<Users> pd = usersService.getUsersByPage(page, limit, content);

		pd.setCode(0);
		pd.setMsg("请求成功");

		Gson gson = new Gson();
		String strpd = gson.toJson(pd);
		out.print(strpd);
		out.close();
	}

	/**
	 * 删除
	 */
	protected void doDelete(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("application/json");
		response.setCharacterEncoding("utf-8");
		int userId = 0;
		if (request.getParameter("userId") != null) {
			userId = Integer.parseInt(request.getParameter("userId"));
		}
		System.out.println(userId);
		boolean flag = usersService.DeleteUser(userId);

		CommonMessage cm = new CommonMessage(flag ? "删除成功" : "删除失败");

		PrintWriter out = response.getWriter();

		Gson gson = new Gson();
		String strcm = gson.toJson(cm);
		out.print(strcm);
		out.close();

	}

	/**
	 * 修改
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void doUpdateUser(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("application/json");
		response.setCharacterEncoding("utf-8");

		StringBuffer myJson = new StringBuffer();
		BufferedReader reader = request.getReader();

		String line = null;
		while ((line = reader.readLine()) != null) {
			myJson.append(line);
		}
		System.out.println(myJson.toString());

		Gson gson = new Gson();

		// 将json字符串转换成java对象
		Users user = gson.fromJson(myJson.toString(), Users.class);

		System.out.println("user:" + user);

		boolean flag = usersService.updateUser(user);
		System.out.println("updateUser:" + user);

		CommonMessage cm = new CommonMessage(flag ? "修改成功" : "修改失败");

		PrintWriter out = response.getWriter();

		String strcm = gson.toJson(cm);

		out.print(strcm);
		out.close();

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
